import 'package:check_app/dao/userInfo_dao.dart';
import 'package:check_app/page/MyCredentialsPage.dart';
import 'package:check_app/page/MyInfoPage.dart';
import 'package:check_app/page/MyPasswordPage.dart';
import 'package:check_app/page/Version.dart';
import 'package:check_app/util/pref_util.dart';
import 'package:check_app/util/uploadImage_util.dart';
import 'package:check_app/widgets/downModal.dart';
import 'package:flutter/material.dart';
import 'package:check_app/page/LoginPage.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';

class MyPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _MyPageState();
}

class _MyPageState extends State<MyPage> {
  bool _loading = false;
  String name;
  String code;
  String mobile;
  String typeName;
  String roleName;
  String avatarUrl;
  String signUrl;
  List<String> certificateUrlList;

  @override
  void initState() {
    super.initState();
    getData();
  }

  Future<Null> getData() async {
    var aid = await PrefUtil().getString('aid');
    var path = '/admin/' + aid.toString();
    setState(() {
      _loading = true;
    });
    try {
      var result = await UserInfoDao.fetch(path, {'aid': aid});
      setState(() {
        _loading = false;
      });
      if (result.success) {
        setState(() {
          name = result.data.name;
          code = result.data.code;
          mobile = result.data.mobile;
          typeName = result.data.typeName;
          roleName = result.data.roleName;
          avatarUrl = result.data.avatarUrl;
          signUrl = result.data.signUrl;
          certificateUrlList = result.data.certificateUrlList;
        });
      } else {
        Fluttertoast.showToast(
          msg: result.message,
        );
      }
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
    }
    return null;
  }

  Future<Null> saveSignUrl(String url) async {
    if (url == null || url == '') return null;
    setState(() {
      _loading = true;
    });
    try {
      var result = await UserInfoDao.save({'signUrl': url});
      setState(() {
        _loading = false;
      });
      if (result.success) {
        setState(() {
          signUrl = url;
        });
      } else {
        Fluttertoast.showToast(
          msg: result.message,
        );
      }
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromRGBO(248, 248, 249, 1),
      body: SafeArea(
        child: ModalProgressHUD(
          inAsyncCall: _loading,
          child: Column(
            children: <Widget>[
              // 个人头像
              GestureDetector(
                onTap: () async {
                  await Navigator.of(context)
                      .push(MaterialPageRoute(builder: (_) {
                    return MyInfoPage(
                      name: name,
                      code: code,
                      typeName: typeName,
                      mobile: mobile,
                      avatarUrl: avatarUrl,
                    );
                  }));
                  getData();
                },
                child: Container(
                  width: double.infinity,
                  height: 128,
                  padding: EdgeInsets.fromLTRB(16, 40, 16, 32),
                  color: Color.fromRGBO(89, 131, 252, 1),
                  child: Row(
                    children: <Widget>[
                      CircleAvatar(
                        radius: 28.0,
                        backgroundImage: avatarUrl == null
                            ? AssetImage('assets/images/touxiang.png')
                            : NetworkImage(avatarUrl),
                      ),
                      Expanded(
                        child: Container(
                          padding: EdgeInsets.fromLTRB(12, 6, 0, 6),
                          child: Column(
                            children: <Widget>[
                              Container(
                                alignment: Alignment.centerLeft,
                                child: Text(
                                  name ?? '--',
                                  style: TextStyle(
                                    fontSize: 18,
                                    height: 25 / 18,
                                    color: Colors.white,
                                  ),
                                ),
                              ),
                              Container(
                                alignment: Alignment.centerLeft,
                                child: Text(
                                  roleName ?? '--',
                                  style: TextStyle(
                                    fontSize: 11,
                                    height: 15 / 11,
                                    color: Color.fromRGBO(255, 255, 255, 0.6),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                      Container(
                        width: 8,
                        height: 13,
                        child: Image(
                          image: AssetImage('assets/images/chevron.png'),
                          width: 8,
                          height: 13,
                        ),
                      ),
                    ],
                  ),
                ),
              ),

              // 电子签名
              GestureDetector(
                onTap: () {
                  showModalBottomSheet(
                    context: context,
                    builder: (BuildContext context) {
                      return DownModal(
                        cameraCb: () async {
                          final url =
                              await UploadImageUtil().getImageFromCamera();
                          saveSignUrl(url);
                        },
                        photoCb: () async {
                          final urlList = await UploadImageUtil().loadAssets(1);
                          if (urlList != null) {
                            saveSignUrl(urlList[0]);
                          }
                        },
                      );
                    },
                  );
                },
                child: Container(
                  width: double.infinity,
                  height: 108,
                  margin: EdgeInsets.only(
                    top: 12,
                    bottom: 12.5,
                  ),
                  padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(255, 255, 255, 1),
                    border: Border(
                      top: BorderSide(
                        width: 0.5,
                        color: Color.fromRGBO(229, 229, 229, 1),
                      ),
                      bottom: BorderSide(
                        width: 0.5,
                        color: Color.fromRGBO(229, 229, 229, 1),
                      ),
                    ),
                  ),
                  child: Row(
                    children: <Widget>[
                      Image(
                        image: AssetImage('assets/images/qianming.png'),
                        width: 20,
                        height: 20,
                      ),
                      Expanded(
                        child: Container(
                          margin: EdgeInsets.only(left: 8),
                          child: Text(
                            '个人电子签名',
                            style: TextStyle(
                              fontSize: 16,
                              height: 22 / 16,
                            ),
                          ),
                        ),
                      ),
                      Container(
                        height: 68,
                        width: 90,
                        margin: EdgeInsets.only(right: 12),
                        padding: EdgeInsets.fromLTRB(13, 7.5, 16, 7.5),
                        decoration: BoxDecoration(
                          border: Border.all(
                            width: 0.5,
                            color: Color.fromRGBO(229, 229, 229, 1),
                          ),
                          borderRadius: BorderRadius.circular(2),
                        ),
                        child: Container(
                          width: 61,
                          height: 53,
                          decoration: BoxDecoration(
                            border: Border.all(
                              width: 0.5,
                              color: Color.fromRGBO(229, 229, 229, 1),
                            ),
                            borderRadius: BorderRadius.circular(2),
                          ),
                          child: avatarUrl == null
                              ? SizedBox()
                              : Image(
                                  image: NetworkImage(signUrl),
                                ),
                        ),
                      ),
                      Image(
                        image: AssetImage('assets/images/chevron_gray.png'),
                        width: 8,
                        height: 13,
                      ),
                    ],
                  ),
                ),
              ),

              // 资格证书
              GestureDetector(
                onTap: () async {
                  await Navigator.of(context)
                      .push(MaterialPageRoute(builder: (_) {
                    return MyCredentialsPage(
                      certificateUrlList: certificateUrlList,
                    );
                  }));
                  getData();
                },
                child: Container(
                  width: double.infinity,
                  height: 62,
                  padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(255, 255, 255, 1),
                    border: Border(
                      top: BorderSide(
                        width: 0.5,
                        color: Color.fromRGBO(229, 229, 229, 1),
                      ),
                      bottom: BorderSide(
                        width: 0.5,
                        color: Color.fromRGBO(229, 229, 229, 1),
                      ),
                    ),
                  ),
                  child: Row(
                    children: <Widget>[
                      Image(
                        image: AssetImage('assets/images/zhengshu.png'),
                        width: 20,
                        height: 20,
                      ),
                      Expanded(
                        child: Container(
                          margin: EdgeInsets.only(left: 8),
                          child: Text(
                            '资格证书',
                            style: TextStyle(
                              fontSize: 16,
                              height: 22 / 16,
                            ),
                          ),
                        ),
                      ),
                      Image(
                        image: AssetImage('assets/images/chevron_gray.png'),
                        width: 8,
                        height: 13,
                      ),
                    ],
                  ),
                ),
              ),

              // 修改密码
              GestureDetector(
                onTap: () {
                  Navigator.of(context).push(MaterialPageRoute(builder: (_) {
                    return MyPasswordPage();
                  }));
                },
                child: Container(
                  width: double.infinity,
                  height: 62,
                  padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(255, 255, 255, 1),
                    border: Border(
                      bottom: BorderSide(
                        width: 0.5,
                        color: Color.fromRGBO(229, 229, 229, 1),
                      ),
                    ),
                  ),
                  child: Row(
                    children: <Widget>[
                      Image(
                        image: AssetImage('assets/images/mima.png'),
                        width: 20,
                        height: 20,
                      ),
                      Expanded(
                        child: Container(
                          margin: EdgeInsets.only(left: 8),
                          child: Text(
                            '修改密码',
                            style: TextStyle(
                              fontSize: 16,
                              height: 22 / 16,
                            ),
                          ),
                        ),
                      ),
                      Image(
                        image: AssetImage('assets/images/chevron_gray.png'),
                        width: 8,
                        height: 13,
                      ),
                    ],
                  ),
                ),
              ),

              // 系统版本
              // GestureDetector(
              //   onTap: () {
              //     Navigator.of(context).push(MaterialPageRoute(builder: (_) {
              //       return Version();
              //     }));
              //   },
              //   child: Container(
              //     width: double.infinity,
              //     height: 62,
              //     padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
              //     decoration: BoxDecoration(
              //       color: Color.fromRGBO(255, 255, 255, 1),
              //       border: Border(
              //         bottom: BorderSide(
              //           width: 0.5,
              //           color: Color.fromRGBO(229, 229, 229, 1),
              //         ),
              //       ),
              //     ),
              //     child: Row(
              //       children: <Widget>[
              //         Image(
              //           image: AssetImage('assets/images/mima.png'),
              //           width: 20,
              //           height: 20,
              //         ),
              //         Expanded(
              //           child: Container(
              //             margin: EdgeInsets.only(left: 8),
              //             child: Text(
              //               '系统版本',
              //               style: TextStyle(
              //                 fontSize: 16,
              //                 height: 22 / 16,
              //               ),
              //             ),
              //           ),
              //         ),
              //         Image(
              //           image: AssetImage('assets/images/chevron_gray.png'),
              //           width: 8,
              //           height: 13,
              //         ),
              //       ],
              //     ),
              //   ),
              // ),

              // 退出登录
              GestureDetector(
                onTap: () {
                  PrefUtil().delete('isLogin');
                  Navigator.of(context).pushAndRemoveUntil(
                      MaterialPageRoute(builder: (_) {
                    return LoginPage();
                  }), (Route<dynamic> route) => false);
                },
                child: Container(
                  width: double.infinity,
                  height: 46,
                  margin: EdgeInsets.only(top: 12),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(255, 255, 255, 1),
                    border: Border(
                      top: BorderSide(
                        width: 0.5,
                        color: Color.fromRGBO(229, 229, 229, 1),
                      ),
                      bottom: BorderSide(
                        width: 0.5,
                        color: Color.fromRGBO(229, 229, 229, 1),
                      ),
                    ),
                  ),
                  child: Text(
                    '退出登录',
                    style: TextStyle(
                      fontSize: 16,
                      height: 22 / 16,
                      color: Color.fromRGBO(240, 64, 64, 1),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
